// 注册组件
Vue.component('v-select', VueSelect.VueSelect);
window.onload=function ()
{


    var vue=new Vue({
        el:"#app",
        data:{
            templateList:[],
            searchTemplate:{},
            pages:0,
            current:1,
            //template:{name:'',specIds:[],brandIds:[],customAttributeItems:[]},
            template:{brandIds:[],specIds:[],customAttributeItems:[]},
            brandList:[],
            specificationList:[],
            ids:[],
            checked:false
        },
        methods:{
            findByPage:function (current)
            {
                axios.get("/typeTemplate/findByPage?page="+current,{params:this.searchTemplate}).then(function (response)
                {
                    vue.templateList=response.data.rows;
                    vue.pages=response.data.pages;
                    vue.current=current;
                })
            },
            findBrandSpecificationList:function ()
            {
                axios.get("/brand/findBrandList").then(function (response)
                {
                    vue.brandList=response.data;
                });
                axios.get("/specification/findSpecificationList").then(function (response)
                {
                    vue.specificationList=response.data;
                    console.log(vue.specificationList);
                })
            },
            /*findSpecificationList:function ()
            {
                axios.get("/specification/findSpecificationList").then(function (response)
                {
                    vue.specificationList=response.data;
                    console.log(vue.specificationList);
                })

            }*/
            addTabRow:function ()
            {
                this.template.customAttributeItems.push({});
            },
            deleteTabRow:function (index)
            {
                this.template.customAttributeItems.splice(index,1);
            },
            show:function (template)
            {
                var templateJson=JSON.stringify(template);
                this.template=JSON.parse(templateJson);
                this.template.brandIds=JSON.parse(this.template.brandIds);
                this.template.specIds=JSON.parse(this.template.specIds);
                this.template.customAttributeItems=JSON.parse(this.template.customAttributeItems);
                //this.template=template;
            },
            saveOrUpdate:function ()
            {
                var url="save";
                if(this.template.id)
                {
                    url="update";
                }
                axios.post("/typeTemplate/"+url,this.template).then(function (response)
                {
                    if(response.data)
                    {
                       vue.findByPage(vue.current);
                    }
                    else
                    {
                        alert("添加/更新失败！");
                    }
                })
            },
            checkAll:function (event)
            {
                this.ids=[];
                if(event.target.checked)
                {
                    for(var i=0;i<this.templateList.length;i++)
                    {
                        this.ids.push(this.templateList[i].id)
                    }
                }

            },
            //提取数组中json某个属性，返回拼接的字符串（逗号分隔）
            json2Arr:function (json)
            {
                var arr=[];
                var jsonArr = JSON.parse(json);
                for(var i=0;i<jsonArr.length;i++)
                {
                    arr.push(jsonArr[i].text);
                }
                //join把数组的元素用逗号分隔，返回字符串
                return arr.join(",");
            },
            del:function ()
            {
                axios.get("/typeTemplate/delete?ids="+this.ids).then(function (response)
                {
                    if(response.data)
                    {
                        vue.current=((vue.checked && vue.current==vue.pages)?vue.current-1 : vue.current);
                        if(vue.current<=0)
                        {
                            vue.current=1;
                        }
                        vue.findByPage(vue.current);
                    }
                    else
                    {
                        alert("删除失败！");
                    }
                })

            }
        },
        created:function ()
        {
            this.findByPage(this.current);
            this.findBrandSpecificationList();
        },
        updated:function ()
        {
            this.checked=(this.ids.length==this.templateList.length);
        }
    })
};